package com.odriver.neulearn.Util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by yeruyi on 2016/11/27.
 */

public class SaveUtil {


    public static SharedPreferences getSharePreferences(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginUserInfo", Context.MODE_PRIVATE);
        return sharedPreferences;
    }

    /**
     * 储存Cookie值进入SharaPreference
     * @param cookie
     */
    public static void saveCookieInSharePreference(Context context, String cookie) {
        SharedPreferences sharedPreferences = SaveUtil.getSharePreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("Cookie", SaveUtil.formalCookie(cookie));
        editor.commit();
    }

    /**
     * 格式化储存Cookie
     * @param cookie
     * @return 格式化后的Cookie
     */

    public static String formalCookie(String cookie) {
        String[] tmp = cookie.split(";");
        return tmp[0] + ";";
    }

    /**
     * 获取Cookie
     * @return Cookie值
     */
    public static String getCookie(Context context){
        SharedPreferences sharedPreferences = SaveUtil.getSharePreferences(context);
        String result = sharedPreferences.getString("Cookie","");
        return result;
    }
}
